#include "ogrebindingPCH.h"
#include "luaclassbinding.h"
#include "ogrebinding.h"
#include "clientbinding.h"
#include "camerabinding.h"
#include "OgreRoot.h"

namespace lua_ogre
{

int AddViewPort(lua_State *ls)
{
	if (lua_gettop(ls) != 1
		|| !luaL_checkudata(ls, 1, CameraClassName))
	{
		LogLuaError << "client.addviewport(camera) failed! parameter is invalid!";
		return 0;
	}
	
	LuaCamera *lua_cam = (LuaCamera*)lua_touserdata(ls, 1);
	if (lua_cam->c_obj == NULL)
	{
		LogLuaError << "client.addviewport(camera) failed, invalid camera which has been destroyed!";
		return 0;
	}

	Ogre::RenderTarget *rt = Ogre::Root::getSingleton().getRenderTarget(MAINWINDOW);
	if (NULL != rt)
	{
		rt->addViewport(lua_cam->c_obj);
	}
	else
	{
		LogLuaError << "main window is not found!";
	}
	return 0;
}

static const luaL_reg client_methods[] = 
{
	{"addviewport", AddViewPort},
	{0, 0}
};

void BindClient(lua_State *ls)
{
	luaL_register(ls, "client", client_methods);
    lua_pop(ls, 1);
}

}